#include "widget.h"

Widget::Widget(QWidget *parent) :
    BaseRestEditor(parent)
{
    setupUi(this);

    selectId=0;
    advanced=false;
    listModel=new QStringListModel(this);
    listView->setModel(listModel);

    tableModel=new TableModel(this);
    tableView->setModel(tableModel);

    move(100,100);
    wakeUp();
    createMenu();
}

Widget::~Widget(){}

void Widget::wakeUp()
{
    if(QFile::exists(M_CONFIG))
    {
        QSettings cfg(M_CONFIG,QSettings::IniFormat);
        if(cfg.value("Screen/full").toInt() == 1)
            showMaximized();

        if(cfg.value("Main/advanced").toInt() == 1)
            advanced=true;
    }
}

void Widget::generateMenu()
{
    menu->addAction(QStringLiteral("打开链接 (Alt+W)"),this,&Widget::do_openUrl);
}

void Widget::onReceivedObject(const QJsonObject &object)
{
    if(!object.contains("entry") || object.value("entry").toString() != entry_)
        return;

    setWindowTitle(_title);
    switch (selectId) {
    case ID_SCHEDULE:
        processSchedule(object);
        break;
    case ID_ASSEMBLE_FLOW:
        processAssembleFlow(object);
        break;
    case ID_ASSEMBLE_LOG:
        processAssembleLog(object);
        break;
    case ID_POSITION:
        processPosition(object);
        break;
    case ID_PURCHASE_LOG:
        processPurchaseLog(object);
        break;
    case ID_PARLOR:
        processParlor(object);
        break;
    }
}

void Widget::processSchedule(const QJsonObject &object)
{
    if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,
                             scheduleColumns,
                             scheduleColumnHeaders);
    }

    if(tableModel->rowCount() > 0)
        tableView->resizeRowsToContents();
}

void Widget::processParlor(const QJsonObject &object)
{
    if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,
                             parlorColumns,
                             parlorColumnHeaders);

        if(tableModel->rowCount() > 0)
            tableView->resizeRowsToContents();
    }
    else if(object.contains("date_list"))
    {
        QJsonArray array=object.value("date_list").toArray();
        parseArray(array);

        QString url=baseUrl_ + "detail";
        emit get(url);
    }
}

void Widget::processAssembleFlow(const QJsonObject &object)
{
    if(object.contains("date_list"))
    {
        QJsonArray array=object.value("date_list").toArray();
        parseArray(array);
    }
    else if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,
                             assembleFlowColumns,
                             assembleFlowColumnHeaders);

        tableView->setColumnWidth(
                    assembleFlowColumns.indexOf("name"),
                    WIDTH_M);
    }
}

void Widget::processAssembleLog(const QJsonObject &object)
{
    if(object.contains("date_list"))
    {
        QJsonArray array=object.value("date_list").toArray();
        parseArray(array);
    }
    else if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,
                             assembleLogColumns,
                             assembleLogColumnHeaders);

        tableView->setColumnWidth(
                    assembleLogColumns.indexOf("name"),
                    WIDTH_L);
    }
}

void Widget::processPosition(const QJsonObject &object)
{
    if(object.contains("topics"))
    {
        QJsonArray array=object.value("topics").toArray();
        parseArray(array);
    }
    else if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,
                             positionColumns,
                             positionColumnHeaders);

        tableView->setColumnWidth(0,WIDTH_M);
        tableView->setColumnWidth(1,WIDTH_M);
    }
}

void Widget::processPurchaseLog(const QJsonObject &object)
{
    if(object.contains("categories"))
    {
        QJsonArray array=object.value("categories").toArray();
        parseArray(array);

        QString url=baseUrl_ + "detail";
        emit get(url);
    }
    else if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        tableModel->setArray(array,
                             purchaseLogColumns,
                             purchaseLogColumnHeaders);

        tableView->setColumnWidth(1,WIDTH_M);
        tableView->setColumnWidth(2,WIDTH_M);
    }
}

void Widget::parseArray(const QJsonArray &array)
{
    if(array.isEmpty())
        return;

    Piv piv(array);
    QStringList items=piv.getStringList();
    if(!items.isEmpty())
        listModel->setStringList(items);
}

void Widget::on_btn_schedule_clicked()
{
    selectId=ID_SCHEDULE;
    entry_=T_SCHEDULE;
    baseUrl_=URL_SCHEDULE;
    _title=TLE_SCHEDULE;

    if(this->listView->isEnabled())
        this->listView->setEnabled(false);

    if(listModel->rowCount() != 0)
        listModel->setStringList(QStringList());

    if(this->kw->isEnabled())
        this->kw->setEnabled(false);

    updateTableView();
}

void Widget::on_btn_parlor_clicked()
{
    selectId=ID_PARLOR;
    entry_=T_PARLOR;
    baseUrl_=URL_PARLOR;
    _title=TLE_PARLOR;

    if(!this->listView->isEnabled())
        this->listView->setEnabled(true);

    if(!this->kw->isEnabled())
        this->kw->setEnabled(true);

    updateTableView();
}

void Widget::on_btn_purchase_clicked()
{
    selectId=ID_PURCHASE_LOG;
    entry_=T_PURCHASE_LOG;
    baseUrl_=URL_PURCHASE_LOG;
    _title=TLE_PURCHASE_LOG;

    if(!this->listView->isEnabled())
        this->listView->setEnabled(true);

    if(!this->kw->isEnabled())
        this->kw->setEnabled(true);

    updateTableView();
}

void Widget::on_btn_assemblelog_clicked()
{
    selectId=ID_ASSEMBLE_LOG;
    entry_=T_ASSEMBLE_LOG;
    baseUrl_=URL_ASSEMBLE_LOG;
    _title=TLE_ASSEMBLE_LOG;

    if(!this->listView->isEnabled())
        this->listView->setEnabled(true);

    if(this->kw->isEnabled())
        this->kw->setEnabled(false);

    updateTableView();
}

void Widget::on_btn_assembleflow_clicked()
{
    selectId=ID_ASSEMBLE_FLOW;
    entry_=T_ASSEMBLE_FLOW;
    baseUrl_=URL_ASSEMBLE_FLOW;
    _title=TLE_ASSEMBLE_FLOW;

    if(!this->listView->isEnabled())
        this->listView->setEnabled(true);

    if(!this->kw->isEnabled())
        this->kw->setEnabled(true);

    updateTableView();
}

void Widget::on_btn_position_clicked()
{
    selectId=ID_POSITION;
    entry_=T_POSITION;
    baseUrl_=URL_POSITION;
    _title=TLE_POSITION;

    if(!this->listView->isEnabled())
        this->listView->setEnabled(true);

    if(!this->kw->isEnabled())
        this->kw->setEnabled(true);

    updateTableView();
}

void Widget::updateTableView()
{
    if(listModel->rowCount() != 0)
        listModel->setStringList(QStringList());

    if(tableModel->rowCount() != 0)
        tableModel->clear();

    QString url;
    switch (selectId) {
    case ID_SCHEDULE:
        url=baseUrl_+ "get_detail";
        break;
    case ID_ASSEMBLE_FLOW:
        url=baseUrl_ + "get_dates/" + CurrentMonth;
        break;
    case ID_ASSEMBLE_LOG:
        url=baseUrl_ + "get_dates/" + CurrentMonth;
        break;
    case ID_POSITION:
        url=baseUrl_ + "get_topics";
        break;
    case ID_PURCHASE_LOG:
        url=baseUrl_ + "get_categories";
        break;
    case ID_PARLOR:
        url=baseUrl_+ "get_dates/" + CurrentMonth;
        break;
    }

    if(!url.isEmpty())
        emit get(url);
}

void Widget::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QString cell=index.data().toString();
        if(cell.isEmpty())
            return;

        QString url;
        switch (selectId) {
        case ID_ASSEMBLE_FLOW:
            url=baseUrl_ + "by_date/" + cell;
            break;
        case ID_ASSEMBLE_LOG:
            url=baseUrl_ + "by_date/" + cell;
            break;
        case ID_POSITION:
            url=baseUrl_ + "by_topic/" + cell;
            break;
        case ID_PURCHASE_LOG:
            url=baseUrl_ + "by_category/" + cell;
            break;
        case ID_PARLOR:
            url=baseUrl_ + "by_date/" + cell;
            break;
        }
        emit get(url);
    }
}

void Widget::on_kw_returnPressed()
{
    if(baseUrl_.isEmpty())
        return;

    QString kw=this->kw->text().trimmed();
    if(kw.isEmpty())
        return;

    QString url=baseUrl_ + "search/" + kw;
    emit get(url);
    this->kw->clear();
}

void Widget::on_tableView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        if(selectId != ID_SCHEDULE || selectId != ID_PARLOR)
            return;

        QModelIndex dateIndex=index.sibling(index.row(),0);
        QString date=dateIndex.data().toString();
        QModelIndex detailIndex=index.sibling(index.row(),1);
        QString text=detailIndex.data().toString();
        if(date.isEmpty() || text.isEmpty())
            return;

        qApp->clipboard()->setText(date + " " + text);
    }
}
